package com.dp.service.menucontrol;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.pojo.JsonPage;
import com.dp.service.impl3.BaseImpl;
import com.dp.util.DataMap;
import com.dp.vo.NewsK16MenuItemUrl;

@Service
@Transactional
public class K16menuitemurlServiceImpl extends BaseImpl {
	
	/**
	 * 智能多媒体电话机，菜单项名称与视频url映射。列表实体对象集合；
	 * @param hashMap
	 * @param firstrow
	 * @param maxrow
	 * @return List<NewsK16MenuItemUrl>
	 */
	public List<NewsK16MenuItemUrl> findAllK16menuitemurlEntity(DataMap hashMap, Integer firstrow,
			Integer maxrow){
		JsonPage jpage=(JsonPage) hashMap.get("jpage");
		NewsK16MenuItemUrl k16menuitemurl=(NewsK16MenuItemUrl) hashMap.get("k16menuitemurl");
		StringBuffer sqlS = new StringBuffer("SELECT n FROM NewsK16MenuItemUrl n WHERE 1=1 ");
		if(k16menuitemurl!=null){
			if(k16menuitemurl.getItemname()!=null&& k16menuitemurl.getItemname().toString().trim().length() > 0){
				sqlS.append(" AND n.itemname like '%"
						+ k16menuitemurl.getItemname() + "%'");
			}
			if(k16menuitemurl.getUrl()!=null&& k16menuitemurl.getUrl().toString().trim().length() > 0){
				sqlS.append(" AND n.url like '%"
						+ k16menuitemurl.getUrl() + "%'");
			}
		}
		if(jpage!=null){
			if(jpage.getSidx()!=null&&!"".equals(jpage.getSidx())){
				sqlS.append(" order by n."+jpage.getSidx()+" "+jpage.getSord());
			}
		}
		List<NewsK16MenuItemUrl> queryList=new ArrayList<NewsK16MenuItemUrl>();
		try {
			Query query = em.createQuery(sqlS.toString());
			if (firstrow != null) {
				query.setFirstResult(firstrow);
			}
			if (maxrow != null) {
				query.setMaxResults(maxrow);
			}
			queryList = query.getResultList();
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		return queryList;
	}
	
	/**
	 * 智能多媒体电话机，菜单项名称与视频url映射。总记录数；
	 * @param k16menuitemurl.itemname
	 * @return totalcount
	 */
	public Integer findAllK16menuitemurlEntityCount(DataMap hashMap){
		Integer iCount = 0; 
		NewsK16MenuItemUrl k16menuitemurl=(NewsK16MenuItemUrl) hashMap.get("k16menuitemurl");
		StringBuffer sqlS = new StringBuffer("SELECT count(n.id) FROM NewsK16MenuItemUrl n WHERE 1=1 ");
		if(k16menuitemurl!=null){
			if(k16menuitemurl.getItemname()!=null&& k16menuitemurl.getItemname().toString().trim().length() > 0){
				sqlS.append(" AND n.itemname like '%"
						+ k16menuitemurl.getItemname() + "%'");
			}
			if(k16menuitemurl.getUrl()!=null&& k16menuitemurl.getUrl().toString().trim().length() > 0){
				sqlS.append(" AND n.url like '%"
						+ k16menuitemurl.getUrl() + "%'");
			}
		}
		Query query=null;
		try {
			query = em.createQuery(sqlS.toString());
			iCount = Integer.valueOf(query.getSingleResult().toString());
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		return iCount;
	}
	
	/**
	 * 新增时判断此菜单项名称是否已存在映射关系
	 * @param itemname
	 * @return
	 */
	public boolean checkItemname(String itemname) {
		boolean b=false;
		try {
			StringBuffer sf = new StringBuffer("SELECT n FROM NewsK16MenuItemUrl n WHERE n.itemname=:itemname");
			Query query = em.createQuery(sf.toString());
			query.setParameter("itemname", itemname);
			if(query.getResultList().size()>0){
				b=true;
			}
		} catch (Exception e) {
			logger.warn(e.getMessage());
		}
		return b;
	}
	
	/**
	 * 修改时判断新变化菜单项名称是否已存在映射关系
	 * @param itemname
	 * @return
	 */
	public boolean checkItemname2(String itemname,String itemnameO) {
		boolean b=false;
		try {
			StringBuffer sf = new StringBuffer("SELECT n FROM NewsK16MenuItemUrl n WHERE n.itemname=:itemname and n.itemname!=:itemnameO");
			Query query = em.createQuery(sf.toString());
			query.setParameter("itemname", itemname);
			query.setParameter("itemnameO", itemnameO);
			if(query.getResultList().size()>0){
				b=true;
			}
		} catch (Exception e) {
			logger.warn(e.getMessage());
		}
		return b;
	}
	
	public NewsK16MenuItemUrl findK16MenuItemUrlByName(String itemname) {
		NewsK16MenuItemUrl o = null;
		try {
			o = this.em.find(NewsK16MenuItemUrl.class, itemname);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		if (o == null) {
			String msg = "Uh oh, '" + NewsK16MenuItemUrl.class + "' object with id '" + itemname
					+ "' not found...";
			logger.warn(msg);
			throw new EntityNotFoundException(msg);
		}
	
		return o;
	}
	
	//修改
	public boolean updateK16menuitemurl(NewsK16MenuItemUrl k16menuitemurl,String itemnameO) {
		boolean b=true;
		try {
			StringBuffer sf = new StringBuffer("update NewsK16MenuItemUrl set itemname=:itemname,url=:url where itemname=:itemnameO");
			Query query = em.createQuery(sf.toString());
			query.setParameter("itemname", k16menuitemurl.getItemname());
			query.setParameter("url", k16menuitemurl.getUrl());
			query.setParameter("itemnameO", itemnameO);
			query.executeUpdate();
		} catch (Exception e) {
			logger.warn(e.getMessage());
			b=false;
		}
		return b;
	}
	
	//删除
	public boolean deleteK16menuitemurl(String ids) {
		Boolean resultb=true;
		try {
			
			String[] idarys=ids.split(",");
			this.sql="delete from news_menunameurl where menuname=?";
			Query	query=em.createNativeQuery(sql);
				
			for(String menuname:idarys){
				query.setParameter(1, menuname);
				query.executeUpdate();
			}
			
		} catch (Exception e) {
			logger.error("deleteK16menuitemurl fail !!!" + e.getMessage());
			e.printStackTrace();
			resultb=false;
		}
		return resultb;
	}
}
